<template>
    <el-autocomplete v-model="infoName"
                     valueKey="name"
                     @blur="checkCurrentId"
                     :trigger-on-focus="true"
                     :fetch-suggestions="querySearchAsync"
                     @mouseenter.native="isMouseEnter=true"
                     @mouseleave.native="isMouseEnter=false"
                     :disabled="disabled"
                     :placeholder="placeholder" @select="handleSelectDic">
        <i class="el-input__icon" :class="infoName&&isMouseEnter&&!disabled?'el-icon-error':'el-icon-edit'"
           @click="handleClear"
           slot="suffix"></i>
        <template slot-scope="props">
            <div class="name">{{ props.item[searchName] }}</div>
        </template>
    </el-autocomplete>
</template>

<script>
    import CommonSearchInput from './common'
    import {
        getJobMachineryList,
        getJobProfessionList,
        getJobTerminalList,
        getWorkTeamList,
        getOperatorList,
        getTally
    } from "@/api/baseResource/baseResource";

    export default {
        mixins: [CommonSearchInput],
        props: {
            searchType: {
                type: String,
                required: true
            },
            searchName: {
                type: String,
                default: '',
            },
            deptId: '',
            valid: null,
            tall: {
                type: Boolean,
                default: false
            }
        },
        methods: {
            // 异步搜索信息
            querySearchAsync(queryString, cb) {
                let params = {
                    [`search_LIKE_${this.searchName}`]: queryString,
                    'iDisplayLength': this.iDisplayLength
                }
                let searchList
                if (this.searchType === 'JOB_JX') {
                    searchList = getJobMachineryList
                }
                if (this.searchType === 'JOB_ZD') {
                    searchList = getJobTerminalList
                }
                if (this.searchType === 'JOB_RY') {
                    searchList = getJobProfessionList
                }
                if (this.searchType === 'JOB_ZY') {
                    searchList = getWorkTeamList
                }
                if (this.searchType === 'JOB_ZYRY') {
                    searchList = getOperatorList
                }
                if (this.deptId) {
                    params.search_EQ_deptId = this.deptId
                }
                if (this.valid !== null) {
                    params.search_EQ_valid = this.valid
                }
                if (this.tall) {
                    searchList = getTally
                }
                searchList(params).then((reponse) => {
                    cb(reponse.data.aaData)
                })
            },
            handleClear() {
                if (this.infoName && !this.disabled) {
                    this.infoName = ''
                    this.$emit('select', '')
                    this.$emit('selectName', '')
                    this.$emit('selectId', '')
                    this.$emit('matchRecord', '')
                }
            },
            handleSelectDic(item) {
                this.$emit('matchRecord', Object.assign({}, item))
                this.$emit('select', item[this.searchName])
                this.$emit('selectName', item[this.searchName])
                this.$emit('selectId', item.id)
                this.infoName = item[this.searchName]
            },
        },
    }
</script>
